package com.mdhw.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import com.mdhw.system.entity.SysRole;
import com.mdhw.system.entity.SysRoleMenu;
import com.mdhw.system.service.SysRoleMenuService;
import com.mdhw.system.service.SysRoleService;
import com.mdhw.system.service.SysUserService;
import com.mdhw.system.vo.SysMenuTree;
import com.mdhw.system.vo.SysMenuVo;
import com.mdhw.system.vo.SysUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdhw.system.dao.SysMenuDao;
import com.mdhw.system.entity.SysMenu;
import com.mdhw.system.service.SysMenuService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Huang
* @date 2022-03-14
*/

@Slf4j
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;


    @Override
    public List<SysMenuTree> selectMenuTree(Integer filterType,List<Long> checkedIdList) {
        // 查询所有菜单
        LambdaQueryWrapper<SysMenu> smLqw = new LambdaQueryWrapper<>();
        smLqw.eq(SysMenu::getStatus,StatusEnum.OK.getCode().toString());
        if(filterType != null){
            smLqw.ne(SysMenu::getType,filterType);
        }
        List<SysMenu> sysMenuList = this.list(smLqw);
        List<SysMenuTree> sysMenuTreeList = new ArrayList<>();
        List<SysMenuTree> parent0SysMenuTreeList = new ArrayList<>();

        for(SysMenu sysMenu : sysMenuList){
            Long menuId = sysMenu.getId();
            SysMenuTree sysMenuTree = new SysMenuTree();
            BeanUtils.copyProperties(sysMenu,sysMenuTree);
            sysMenuTreeList.add(sysMenuTree);
            if(checkedIdList.contains(menuId)){
                sysMenuTree.setChecked(Boolean.TRUE);
            }else{
                sysMenuTree.setChecked(Boolean.FALSE);
            }
            if(sysMenu.getParentId() == 0){
                parent0SysMenuTreeList.add(sysMenuTree);
            }
        }


        for(SysMenuTree sysMenuTree:parent0SysMenuTreeList){
            List<SysMenuTree> child = getChild(sysMenuTree.getId(), sysMenuTreeList);
            sysMenuTree.setChildren(child);
        }
        return parent0SysMenuTreeList;
    }

    public List<SysMenuTree> getChild(Long parentId,List<SysMenuTree> sysMenuList){
        // 子菜单列表
        List<SysMenuTree> childList = new ArrayList<>();
        for(SysMenuTree sysMenuTree:sysMenuList){
            if(sysMenuTree.getParentId().equals(parentId)){
                childList.add(sysMenuTree);
            }
        }

        // 遍历 获取子菜单的子菜单
        for (SysMenuTree menu : childList) {
            List<SysMenuTree> child = getChild(menu.getId(), sysMenuList);
            menu.setChildren(child);
        }
        //递归出口 childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    /**
     * 新增菜单
     * @param sysMenuVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R<String> addSysMenu(SysMenuVo sysMenuVo) {
        LambdaQueryWrapper<SysMenu> smLqw = new LambdaQueryWrapper<>();
        smLqw.eq(SysMenu::getStatus,StatusEnum.OK.getCode());
        smLqw.eq(SysMenu::getCode,sysMenuVo.getCode());
        List<SysMenu> sysMenuList = this.list(smLqw);
        if(sysMenuList != null && sysMenuList.size() > 0){
            return R.error("新增失败! code重复!");
        }
        Date nowTime = new Date();
        SysUserVo sysUserVo = this.sysUserService.getUserVoByToken();
        String loginName = sysUserVo.getLoginName();
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuVo,sysMenu);
        sysMenu.setCreateDate(nowTime);
        sysMenu.setUpdateDate(nowTime);
        sysMenu.setCreateUser(loginName);
        sysMenu.setUpdateUser(loginName);
        sysMenu.setStatus(StatusEnum.OK.getCode().toString());
        sysMenu.setRevision(0);
        if(sysUserVo.getSortNum() == null){
            sysMenu.setSortNum(0);
        }
        if(sysMenuVo.getParentId() == null){
            sysMenu.setParentId(0L);
        }
        boolean isSuccess = this.save(sysMenu);

        Long sysMenuId = sysMenu.getId();

            LambdaQueryWrapper<SysRole> srLqw = new LambdaQueryWrapper<>();
            srLqw.eq(SysRole::getCode,"admin");
            srLqw.eq(SysRole::getStatus,StatusEnum.OK.getCode());
            List<SysRole> sysRoleList = this.sysRoleService.list(srLqw);
            if(sysRoleList != null && sysRoleList.size() > 0){
                SysRole sysRole = sysRoleList.get(0);
                Long roleId = sysRole.getId();
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(sysMenuId);
                sysRoleMenu.setRoleId(roleId);
                this.sysRoleMenuService.save(sysRoleMenu);
            }


        if(isSuccess){
            return R.success("新增成功");
        }else{
            return R.error("新增失败");
        }
    }

    @Override
    public R<String> updateSysMenu(SysMenuVo sysMenuVo){
        String code = sysMenuVo.getCode();
        Long id = sysMenuVo.getId();
        LambdaQueryWrapper<SysMenu> smLqw = new LambdaQueryWrapper<>();
        smLqw.eq(SysMenu::getStatus,StatusEnum.OK.getCode());
        smLqw.eq(SysMenu::getCode,code);
        smLqw.ne(SysMenu::getId,id);
        List<SysMenu> sysMenuList = this.list(smLqw);
        if(sysMenuList != null && sysMenuList.size() > 0){
            return R.error("新增失败! code重复!");
        }else{
            SysMenu nativeSysMenu = this.getById(id);

            SysMenu sysMenu = new SysMenu();
            BeanUtils.copyProperties(sysMenuVo,sysMenu);
            sysMenu.setCreateUser(nativeSysMenu.getCreateUser());
            sysMenu.setCreateDate(nativeSysMenu.getCreateDate());
            sysMenu.setStatus(nativeSysMenu.getStatus());
            sysMenu.setSortNum(nativeSysMenu.getSortNum());
            sysMenu.setRevision(nativeSysMenu.getRevision());
            boolean isSuccess = this.updateById(sysMenu);
            if(isSuccess){
                return R.success("修改成功");
            }else{
                return R.error("修改失败");
            }
        }
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R<String> deleteSysMenu(Long id) {
        SysMenu sysMenu = this.getById(id);
        sysMenu.setStatus(StatusEnum.DELETE.getCode());
        boolean isSuccess = this.updateById(sysMenu);
        if(isSuccess){
            LambdaQueryWrapper<SysRoleMenu> srmLqw = new LambdaQueryWrapper<>();
            srmLqw.eq(SysRoleMenu::getMenuId,id);
            this.sysRoleMenuService.remove(srmLqw);
        }
        if(isSuccess){
            return R.success("删除成功!");
        }else{
            return R.error("删除失败!");
        }
    }
}